package tech.wenxu.ssoprovider.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.wenxu.ssoprovider.common.SysResult;
import tech.wenxu.ssoprovider.pojo.User;
import tech.wenxu.ssoprovider.service.UserService;

/**
 * @author zhaowenxu
 * @date 2020/4/13 15:43
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/check/{param}/{type}")
    public SysResult<Integer> check(@PathVariable String param, @PathVariable Integer type) {
        int resultCount = userService.check(param, type);
        return SysResult.ok(resultCount);
    }

    @PostMapping("/register")
    public SysResult<Integer> register(User user) {
        int resultCount = userService.addUser(user);
        return SysResult.ok(resultCount);
    }

    @PostMapping("/login")
    public SysResult<String> login(String username, String password) {
        String ticket = userService.login(username, password);
        return SysResult.ok(ticket);
    }

    @GetMapping("/query/{ticket}")
    public SysResult<User> queryUser(@PathVariable String ticket) {
        User user = userService.queryUser(ticket);
        return SysResult.ok(user);
    }
}
